package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateInfoDTO;
import cn.tedu.csmall.product.pojo.entity.AttributeTemplate;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "03属性管理模版")
@Slf4j
@RestController
@RequestMapping("/attribute-templates")
public class AttributeTemplateController {

    @Autowired
    private IAttributeTemplateService iAttributeTemplateService;

    private AttributeTemplateController(){
        log.debug("创建控制器：AttributeTemplateController");
    }
    // http://localhost:8080/add-new-at?name=128g
    @ApiOperation("添加属性模版")
    @PostMapping("/add-new")
    private String addNew(AttributeTemplateDTO attributeTemplateDTO){
        log.debug("开始处理添加品牌模板的请求; {}",attributeTemplateDTO);
        try {
            iAttributeTemplateService.addNew(attributeTemplateDTO);
            log.debug("添加成功");
            return "添加成功";
        }catch (ServiceException e){
            String message = e.getMessage();
            log.debug(message);
            return message;
        }
    }

    @ApiOperation("根据id删除属性模版")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "属性模版id",required = true,dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    private JsonResult delete(@Range(min = 1,message = "删除品牌失败,尝试删除相册的ID无效")
                              @PathVariable Long id){
        log.debug("开始处理删除属性模板的请求; {}",id);
        iAttributeTemplateService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询属性模版列表")
    @ApiOperationSupport(order = 300)
    @GetMapping("")
    private JsonResult<List<AttributeTemplateListItemVO>> list(){
        log.debug("开始处理查询属性模板的请求;");
        final List<AttributeTemplateListItemVO> list = iAttributeTemplateService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据id查询属性详情")
    @ApiOperationSupport(order = 310)
    @GetMapping("/{id:[0-9]+}/select")
    public JsonResult<AttributeTemplateStandardVO> selectById(@PathVariable Long id){
        AttributeTemplateStandardVO attributeTemplateStandardVO = iAttributeTemplateService.selectById(id);
        return JsonResult.ok(attributeTemplateStandardVO);
    }

    @ApiOperation("修改详情")
    @ApiOperationSupport(order = 500)
    @GetMapping("/update")
    public JsonResult<Void> update(AttributeTemplateUpdateInfoDTO attributeTemplateUpdateInfoDTO){
        iAttributeTemplateService.update(attributeTemplateUpdateInfoDTO);
        return JsonResult.ok();
    }

}
